//---------------------------------------------------------------------------
// Name:	IAPmgr.h
// Desc:	Source of IAPmgr
// Author:	D.C
// Date:	15:54 2009-11-21
//---------------------------------------------------------------------------
#include <s32file.h>
#include <commdb.h>
#include <bautils.h>
#include <apaccesspointitem.h>
#include <apdatahandler.h>
#include <es_sock.h>
#include <commdbconnpref.h>
#include <in_sock.h>
#include "iapmgr.h"

void Create_IAPs()
{
	TInt err=KErrNone;
	TInt aWapID=-1,aNetID=-1;
	CCommsDatabase* db=CCommsDatabase::NewL();
	CApDataHandler* handler=CApDataHandler::NewLC(*db);
	aNetID=IAP_By_Name(KNETIAP);
	if(-1==aNetID)
	{
		CApAccessPointItem* aItem_Net=CApAccessPointItem::NewLC();
		aItem_Net->SetNamesL(_L("meteor_net"));
		aItem_Net->SetBearerTypeL(EApBearerTypeGPRS);
		aItem_Net->WriteBool(EApGprsDisablePlainTextAuth,EFalse);
		aItem_Net->WriteLongTextL(EApGprsAccessPointName,_L("cmnet"));
		TRAP(err,aNetID=handler->CreateFromDataL(*aItem_Net));
		CleanupStack::PopAndDestroy();
	}
	aWapID=IAP_By_Name(KWAPIAP);
	if(-1==aWapID)
	{
		CApAccessPointItem* aItem_Wap=CApAccessPointItem::NewLC();
		aItem_Wap->SetNamesL(_L("meteor_wap"));
		aItem_Wap->SetBearerTypeL(EApBearerTypeGPRS);
		aItem_Wap->WriteBool(EApGprsDisablePlainTextAuth,EFalse);
		aItem_Wap->WriteLongTextL(EApGprsAccessPointName,_L("cmwap"));
		aItem_Wap->WriteBool(EApProxyUseProxy,ETrue);
		aItem_Wap->WriteLongTextL(EApProxyServerAddress,_L("10.0.0.172"));
		aItem_Wap->WriteUint(EApProxyPortNumber,80);
		TRAP(err,aWapID=handler->CreateFromDataL(*aItem_Wap));
		CleanupStack::PopAndDestroy();
	}
	CleanupStack::PopAndDestroy();
}
void Load_IAPs(CDesCArray* aItems)
{
	if (NULL==aItems)
		return;
	aItems->Reset();
	//1.load iap
	CCommsDatabase* db=NULL;
	CCommsDbTableView* view=NULL;
	TBuf<KCommsDbSvrMaxColumnNameLength>iapName;
	TUint32 iapId;
	TInt result;
	//open database
	db=CCommsDatabase::NewL(EDatabaseTypeIAP);
	CleanupStack::PushL(db);
	view=db->OpenTableLC(TPtrC(IAP)); //IAP is the table name
	//walk through records
	result=view->GotoFirstRecord();
	TInt nCount=0;
	while((result==KErrNone)&&(nCount<30))
	{
		view->ReadTextL(TPtrC(COMMDB_NAME),iapName);
		view->ReadUintL(TPtrC(COMMDB_ID),iapId);
		aItems->AppendL(iapName);
		nCount++;
		result=view->GotoNextRecord();
	}
	CleanupStack::PopAndDestroy(2);
}

TInt IAP_By_Name(const TDesC& aIapName)
{
	//get iap id
	CCommsDatabase* db=NULL;
	CCommsDbTableView* view=NULL;
	TBuf<KCommsDbSvrMaxColumnNameLength>iapName;
	TUint32 iapId;
	TInt result;
	//open database
	db=CCommsDatabase::NewL(EDatabaseTypeIAP);
	CleanupStack::PushL(db);
	view=db->OpenTableLC(TPtrC(IAP)); //IAP is the table name
	//walk through records
	result=view->GotoFirstRecord();
	TInt nCount=0;
	TInt iap=-1;
	while((result==KErrNone)&&(nCount<30))
	{
		view->ReadTextL(TPtrC(COMMDB_NAME),iapName);
		view->ReadUintL(TPtrC(COMMDB_ID),iapId);
		if (0==aIapName.Compare(iapName))
		{
			iap=iapId;
			break;
		}
		nCount++;
		result=view->GotoNextRecord();
	}
	CleanupStack::PopAndDestroy(2);
	return iap;
}

TInt Load_IAP_ID(TDesC& aFile)
{
	RFs fileSession;
	TInt err=KErrNone;
	TInt aIap=-1;
	err=fileSession.Connect();
	if (KErrNone!=err)
	{
		fileSession.Close();
		return -1;
	}
	BaflUtils::EnsurePathExistsL(fileSession,aFile);
	RFile file;
	if (file.Open(fileSession,aFile,EFileRead)!=KErrNone)
	{
		err=file.Replace(fileSession,aFile,EFileWrite);
		if (err==KErrNone)
		{
			RFileWriteStream outputFileStream(file);
			CleanupClosePushL(outputFileStream);
			outputFileStream.WriteInt32L(aIap);
			CleanupStack::PopAndDestroy();
		}
		else
		{
			file.Close();
			fileSession.Close();
			return -1;
		}
	}
	else
	{
		RFileReadStream inputFileStream(file);
		CleanupClosePushL(inputFileStream);
		aIap=inputFileStream.ReadInt32L();
		CleanupStack::PopAndDestroy();
	}
	file.Close();
	fileSession.Close();
	return aIap;
}

void Save_IAP_ID(TDesC& aFile,TInt aIap)
{
	RFs fileSession;
	TInt err=KErrNone;
	err=fileSession.Connect();
	if (KErrNone!=err)
	{
		fileSession.Close();
		return;
	}
	BaflUtils::EnsurePathExistsL(fileSession,aFile);
	RFile file;
	if (KErrNone==file.Replace(fileSession,aFile,EFileWrite))
	{
		RFileWriteStream outputFileStream(file);
		CleanupClosePushL(outputFileStream);
		outputFileStream.WriteInt32L(aIap);
		CleanupStack::PopAndDestroy();
		file.Close();
		fileSession.Close();
		return;
	}
	else
	{
		if (KErrNone==file.Open(fileSession,aFile,EFileWrite))
		{
			RFileWriteStream outputFileStream(file);
			CleanupClosePushL(outputFileStream);
			outputFileStream.WriteInt32L(aIap);
			CleanupStack::PopAndDestroy();
		}
	}
	file.Close();
	fileSession.Close();
}

TInt GetProxy(TUint32 iap,TDes& proxy,TUint32& port,TBool& isgprs)
{
	isgprs=ETrue;
	proxy.Zero();
	port=0;

	TInt result=KErrNone;
	TBool bok=EFalse;
	//1.Is GRPS ?  
	CCommsDatabase* db=CCommsDatabase::NewL(EDatabaseTypeIAP);
	TUint32 id=0;
	TBuf<KCommsDbSvrMaxColumnNameLength>iapName;
	TUint32 iapService;
	TBuf<KCommsDbSvrMaxColumnNameLength>serviceType;
	CCommsDbTableView* view=db->OpenTableLC(TPtrC(IAP)); //IAP is the table name
	result=view->GotoFirstRecord();
	while(result==KErrNone)
	{
		view->ReadUintL(TPtrC(COMMDB_ID),id);
		view->ReadTextL(TPtrC(COMMDB_NAME),iapName);
		view->ReadUintL(TPtrC(IAP_SERVICE),iapService);
		view->ReadTextL(TPtrC(IAP_SERVICE_TYPE),serviceType);
		if (iap==id)
		{
			if (0==serviceType.Compare(TPtrC(OUTGOING_GPRS)))
			{
				isgprs=ETrue;
			}
			else
			{
				isgprs=EFalse;
			}
			bok=ETrue;
			break;
		}
		result=view->GotoNextRecord();
	}
	CleanupStack::PopAndDestroy();
	SAFE_DELETE(db);
	if (!bok)
	{
		return KErrNotFound;
	}
	//2.Get Proxy	
	CCommsDatabase* commsDb=CCommsDatabase::NewL(EDatabaseTypeIAP);
	CCommsDbTableView* commsView=commsDb->OpenViewOnProxyRecordLC(iapService,serviceType);
	result=commsView->GotoFirstRecord();
	if (KErrNone!=result)
	{
		CleanupStack::PopAndDestroy();
		SAFE_DELETE(commsDb);
		return KErrNotFound;
	}
	TBool proxyEnabled(EFalse);
	commsView->ReadBoolL(TPtrC(PROXY_USE_PROXY_SERVER),proxyEnabled);
	if (!proxyEnabled)
	{
		CleanupStack::PopAndDestroy();
		SAFE_DELETE(commsDb);
		return KErrNotFound;
	}
	proxy.Copy(*commsView->ReadLongTextLC(TPtrC(PROXY_SERVER_NAME)));
	commsView->ReadUintL(TPtrC(PROXY_PORT_NUMBER),port);
	CleanupStack::PopAndDestroy(2);
	SAFE_DELETE(commsDb);
	return KErrNone;
}

